/*
* Author: Sowmya Kameswaran
*/
package com.foo.AnalyzedData;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


import com.foo.Entity.RealTimeData;
import com.foo.TextDataSender.DataSender;

/**
 * Session Bean implementation class AnalyzedDataProvider
 */
@Stateless(name="AnalyzedDataProvider")
public class AnalyzedDataProvider implements AnalyzedDataProviderRemote, AnalyzedDataProviderLocal {


	@PersistenceContext(unitName="analysisinformationprovider") // This is the entity manager required by the Entity bean
	private EntityManager em;

	int total =0 ;
	Query getanalyzeddata = null;
	List<RealTimeData> analyzeddatalist = null;
	String analyzedmessage="";;
	DataSender datasender = new DataSender();
	/**
	 * Default constructor. 
	 */
	public AnalyzedDataProvider() 
	{
		/*try
		{
			Properties properties = new Properties();
			properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
			properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
			Context ctx = new InitialContext(properties);
			Object ref = ctx.lookup("DataSender/remote");
			datasender = (DataSenderRemote) PortableRemoteObject.narrow(ref, DataSenderRemote.class);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
*/
		System.out.println("In server class to retrieve data \n"); 
	}

	@Override
	public void Analyzedinfo(String exampleName) 
	{
		System.out.println("Entering server function Analyzed Data with parameters " +exampleName);
		
		if(exampleName.equals("TopVolume"))
		{
			analyzedmessage="";
			getanalyzeddata = em.createQuery("select distinct d from RealTimeData d Group by d.ticker_symbol order by d.volume desc");
			getanalyzeddata.setMaxResults(25);			
			analyzeddatalist = getanalyzeddata.getResultList();
			int listsize = analyzeddatalist.size();
			System.out.println("list size "+listsize);
			for(int k=0;k<listsize;k++)
			{
				analyzedmessage += analyzeddatalist.get(k).getTicker_symbol() + "," +analyzeddatalist.get(k).getOpen_value()+","+analyzeddatalist.get(k).getHigh_value()
				+","+analyzeddatalist.get(k).getLow_value()+","+analyzeddatalist.get(k).getClose_value()+","+analyzeddatalist.get(k).getVolume()+"," + analyzeddatalist.get(k).getPerc_change() + ",";
			}
			
		}
		else if(exampleName.equals("TopIncr"))
		{
			analyzedmessage="";
			getanalyzeddata = em.createQuery("select distinct d from RealTimeData d Group by d.ticker_symbol order by d.perc_change desc");
			getanalyzeddata.setMaxResults(25);			
			analyzeddatalist = getanalyzeddata.getResultList();
			int listsize = analyzeddatalist.size();
			System.out.println("list size "+listsize);
			for(int k=0;k<listsize;k++)
			{
				analyzedmessage += analyzeddatalist.get(k).getTicker_symbol() + "," +analyzeddatalist.get(k).getOpen_value()+","+analyzeddatalist.get(k).getHigh_value()
				+","+analyzeddatalist.get(k).getLow_value()+","+analyzeddatalist.get(k).getClose_value()+","+analyzeddatalist.get(k).getVolume()+","+ analyzeddatalist.get(k).getPerc_change() + ",";
			}
		}
		else if(exampleName.equals("TopDecr"))
		{
			analyzedmessage="";
			getanalyzeddata = em.createQuery("select distinct d from RealTimeData d Group by d.ticker_symbol order by d.perc_change");
			getanalyzeddata.setMaxResults(25);			
			analyzeddatalist = getanalyzeddata.getResultList();
			int listsize = analyzeddatalist.size();
			System.out.println("list size "+listsize);
			for(int k=0;k<listsize;k++)
			{
				analyzedmessage += analyzeddatalist.get(k).getTicker_symbol() + "," +analyzeddatalist.get(k).getOpen_value()+","+analyzeddatalist.get(k).getHigh_value()
				+","+analyzeddatalist.get(k).getLow_value()+","+analyzeddatalist.get(k).getClose_value()+","+analyzeddatalist.get(k).getVolume()+","+ analyzeddatalist.get(k).getPerc_change() + ",";
			}
		}
		else
		{
			analyzedmessage="";
		}
		datasender.sendMessage("Analyzed",analyzedmessage);
		System.out.println("The message " +analyzedmessage);
	}		

}
